<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author hancing
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'dp:xinwen:dpXinwen:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    
    <BasicForm @register="registerForm" >
      <template #wzcontent="{ model, field }">
        <MarkDown
          ref="markdownRef"
          v-model:value="model[field]"
          :bizKey="record.id"
          :bizType="'wzenzhang_' + field"
          :height="400"
        />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDpXinwenDpXinwenForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { DpXinwen, dpXinwenSave, dpXinwenForm } from '/@/api/dp/xinwen/dpXinwen';
  import { WangEditor } from '/@/components/WangEditor';
  import { MarkDown } from '/@/components/Markdown';
  const emit = defineEmits(['success', 'register']);

  const md = ref('## hello')
  const markdownRef = ref();
  const { t } = useI18n('dp.xinwen.dpXinwen');
  const { showMessage } = useMessage();
  const record = ref<DpXinwen>({} as DpXinwen);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增文章') : t('编辑文章'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('标题'),
      field: 'title',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('文章类型'),
      field: 'xwtype',
      component: 'Select',
      componentProps: {
        dictType: 'dp_wenzhang_type',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('缩略图'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'dpXinwen_image',
        uploadType: 'image',
      },
    },
    {
      label: t('外部URL'),
      field: 'h5url',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('内容'),
      field: 'wzcontent',
      component: 'InputTextArea',
      slot: 'wzcontent',
      colProps: { lg: 24, md: 24 },
    },
    
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await dpXinwenForm(data);
    record.value = (res.dpXinwen || {}) as DpXinwen;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await dpXinwenSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  function uploadFile(){

  }
</script>
